import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_admin/api/odoo/mrp_production_odoo_api.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/models/odoo/price_trend.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';

class PriceTrendPriceAdd extends StatefulWidget {
  PriceTrendPriceAdd({this.priceId});
  String? priceId;
  @override
  _PriceTrendPriceAddState createState() => _PriceTrendPriceAddState();
}

class _PriceTrendPriceAddState extends State<PriceTrendPriceAdd> {
  PriceTrend priceTrend = PriceTrend();

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  List<SelectOptionVO> priceTypeList = [
    SelectOptionVO(value: '1', label: '合同报价'),
    SelectOptionVO(value: '2', label: '市场价'),
  ];

  @override
  void initState() {
    super.initState();
    this.priceTrend.priceId = widget.priceId;
    _loadSelectVO();
  }

  _loadSelectVO() async {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CrySelect(
            label: '价格类型',
            dataList: priceTypeList,
            value: priceTrend.priceType,
            onSaved: (v) => {priceTrend.priceType = v},
            onChange: (v) async {
              if (priceTrend.priceType != v) {
                formKey.currentState!.save();
                priceTrend.priceType = v;
                setState(() {});
              }
            },
          ),
          // CryInput(
          //   label: '价格',
          //   value: priceTrend.price,
          //   onSaved: (v) {
          //     priceTrend.price = v;
          //   },
          // ),
          CryInputNum(
            label: '价格',
            value: priceTrend.price,
            onSaved: (v) {
              priceTrend.price = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            MrpProductionOdooApi.saveOrUpdate({
              'price_type': priceTrend.priceType,
              'price': priceTrend.price,
              'price_id': priceTrend.priceId,
            }).then((ResponseBodyApi res) {
              if (!res.success!) {
                //CryUtils.message(re s.message!);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(S.of(context).add),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
